using System;
using System.Text;

namespace BuildStatusIndicator.DelCom.Hardware
{
    public class DelComVisualSignalIndicatorHandle : IDelComVisualSignalIndicator
    {
        private readonly StringBuilder _deviceName = new StringBuilder(DelcomVisualIndicatorPIA.MAXDEVICENAMELEN);

        #region IDelComVisualSignalIndicator Members

        public void TurnOnGreen()
        {
            SetLED(DelcomVisualIndicatorPIA.GREENLED, true, false);
            SetLED(DelcomVisualIndicatorPIA.REDLED, false, false);
            SetLED(DelcomVisualIndicatorPIA.YELLOWORBLUELED, false, false);
        }

        public void TurnOff()
        {
            SetLED(DelcomVisualIndicatorPIA.GREENLED, false, false);
            SetLED(DelcomVisualIndicatorPIA.REDLED, false, false);
            SetLED(DelcomVisualIndicatorPIA.YELLOWORBLUELED, false, false);
        }

        public void SetLED(byte led, bool turnOn, bool flash)
        {
            uint deviceHandle = GetDeviceHandle();
            if (deviceHandle == 0)
                throw new Exception("Could not get DelCom device!");

            if (turnOn)
            {
                if (flash)
                    DelcomVisualIndicatorPIA.DelcomLEDControl(deviceHandle, led, DelcomVisualIndicatorPIA.LEDFLASH);
                else
                    DelcomVisualIndicatorPIA.DelcomLEDControl(deviceHandle, led, DelcomVisualIndicatorPIA.LEDON);
            }
            else
            {
                DelcomVisualIndicatorPIA.DelcomLEDControl(deviceHandle, led, DelcomVisualIndicatorPIA.LEDOFF);
            }
            ReleaseHandle(deviceHandle);
        }

        public void TurnOnRed()
        {
            SetLED(DelcomVisualIndicatorPIA.REDLED, true, false);
            SetLED(DelcomVisualIndicatorPIA.GREENLED, false, false);
            SetLED(DelcomVisualIndicatorPIA.YELLOWORBLUELED, false, false);
        }

        public void TurnOnYellowOrBlue()
        {
            SetLED(DelcomVisualIndicatorPIA.YELLOWORBLUELED, true, false);
            SetLED(DelcomVisualIndicatorPIA.GREENLED, false, false);
            SetLED(DelcomVisualIndicatorPIA.REDLED, false, false);
        }

        public void FlashRed()
        {
            SetLED(DelcomVisualIndicatorPIA.REDLED, true, true);  
            SetLED(DelcomVisualIndicatorPIA.GREENLED, false, false);
            SetLED(DelcomVisualIndicatorPIA.YELLOWORBLUELED, false, false);
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            //ReleaseHandle();
        }

        #endregion

        private uint GetDeviceHandle()
        {
            // Search for the first match USB device, For USB IO Chips use Delcom.USBIODS
            // With Generation 2 HID devices, you can pass a TypeId of 0 to open any Delcom device.
            int deviceSearchResult = DelcomVisualIndicatorPIA.DelcomGetNthDevice(DelcomVisualIndicatorPIA.USBDELVI, 0,
                                                                                 _deviceName);
            if (!DeviceFound(deviceSearchResult))
            {
                throw new Exception("DelCom device not found!");
            }

            return DelcomVisualIndicatorPIA.DelcomOpenDevice(_deviceName, 0);
        }

        private void ReleaseHandle(uint deviceHandle)
        {
            DelcomVisualIndicatorPIA.DelcomCloseDevice(deviceHandle);
        }

        private static bool DeviceFound(int deviceSearchResult)
        {
            return deviceSearchResult != 0;
        }

    }
}